package aom.ju.ss.view.main

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.databinding.ViewDataBinding
import aom.ju.ss.BaseActivity2
import aom.ju.ss.R
import aom.ju.ss.databinding.StartPageBinding
import aom.ju.ss.mode.LpKey
import aom.ju.ss.mode.LpKey.ONE_START
import aom.ju.ss.mode.LpKey.TOKEN
import aom.ju.ss.mode.UserInfo
import aom.ju.ss.mode.VersionMode
import aom.ju.ss.utils.BaseApplication
import aom.ju.ss.utils.LogUtils
import aom.ju.ss.utils.SpUtils
import aom.ju.ss.view.base.BaseActivity
import com.bumptech.glide.Glide
import com.google.gson.Gson

class StartPageActivity : BaseActivity2<StartPageBinding>() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding.banben.text = "v" + BaseApplication.getInstance().versionName

//        startUp()
        LogUtils.e(SpUtils.getString(LpKey.USER_INFO))
        LogUtils.e(SpUtils.getString(TOKEN))
        go()
    }
    private fun startUp() {
        if (SpUtils.getString(LpKey.Version_Mode).length > 1) {
            try {
                val c: VersionMode = Gson().fromJson(
                    SpUtils.getString(LpKey.Version_Mode),
                    VersionMode::class.java
                )
                if (c.VersionCode <= BaseApplication.getInstance().appVersion) {
                    var imgstr = ""
                    imgstr = if (BaseApplication.getInstance().resources
                            .displayMetrics.heightPixels > 1920
                    ) {
                        c.StartScreenHigh
                    } else {
                        c.StartScreenWide
                    }
                    if (imgstr != "") {
                        binding.jup.visibility = View.VISIBLE
                        Glide.with(this)
                            .load(imgstr)
                            .centerCrop()
                            .into(binding.img)
                    }
                }
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        getVer()
    }

    private fun getVer() {

//        OKHttpUtils(this, false)
//            .SetApiUrl(LP_API.app_version)
//            .SetKey("platform", "client_id")
//            .SetValue("android", "1")
//            .POST(object : MyHttpCallBack() {
//                override fun ok(jsonString: String?, httpTY: String?) {
//                    val c = Gson().fromJson(jsonString, VersionMode::class.java)
//                    SpUtils.putString(LpKey.Version_Mode, jsonString)
//                    SpUtils.putString("ReviewStatus", c.ReviewStatus))
//                    showImg()
//                }
//
//                override fun error(e: String?, httpTY: String?) {
//                    go()
//                }
//            })
    }

    private fun go() {
        if (UserInfo.userInfo == null) {// 未登录
            val appStallFirst = SpUtils.getInt(ONE_START)
            if (appStallFirst == -1) {
                startActivity(Intent(this, GuideActivity::class.java))
                SpUtils.putInt(ONE_START, 1)
            } else {
                startActivity(Intent(this, LoginActivity::class.java))
            }
            finish()
        } else {
            startActivity(Intent(this, MainActivity::class.java))
            finish()
        }
    }

    private fun imLogin(data: String) {

    }

    fun nimLogin() {
        finish()
//        NimUtils.getInstance().login(
//            activity, data.easemob_username, data.easemob_password, 0
//        ) { success ->
//            if (success) {
//                finish()
//            } else {
//                SpUtils.clearKey(USER_INFO)
//                UserInfo.get().userInfo = null
//                "云信登录失败".show()
//            }
//        }
    }


    private var th: Thread? = null

    private fun showImg() {
        val c = Gson().fromJson(SpUtils.getString(LpKey.Version_Mode), VersionMode::class.java)
        var imgstr: String? = ""
        if (BaseApplication.getInstance().resources.displayMetrics.heightPixels > 1920) {
            imgstr = c.StartScreenHigh
        } else {
            imgstr = c.StartScreenWide
        }
        if (imgstr != null && imgstr != "") {
            binding.jup.visibility = View.VISIBLE
            Glide.with(this)
                .load(imgstr)
                .centerCrop()
                .into(binding.img)
            th = Thread {
                try {
                    Thread.sleep(3000)
                    runOnUiThread { go() }
                } catch (e: InterruptedException) {
                    e.printStackTrace()
                    runOnUiThread { go() }
                }
            }
            th?.start()
        } else {
            go()
        }
    }
}